package com.bootdo.fx.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.common.utils.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.fx.domain.OrderItemDO;
import com.bootdo.fx.service.OrderItemService;

/**
 * 
 * 
 * @author 袁飞龙
 * @email 1992lcg@163.com
 * @date 2017-11-30 17:12:20
 */
 
@Controller
public class OrderItemController {
	@Autowired
	private OrderItemService orderItemService;
	
	@GetMapping("/fx/orderItem")
	@RequiresPermissions("fx:orderItem:orderItem")
	String OrderItem(){
	    return "fx/orderItem/orderItem";
	}
	
	@ResponseBody
	@GetMapping("/fx/orderItem/list")
	@RequiresPermissions("fx:orderItem:orderItem")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<OrderItemDO> orderItemList = orderItemService.list(query);
		int total = orderItemService.count(query);
		PageUtils pageUtils = new PageUtils(orderItemList, total);
		return pageUtils;
	}
	
	@GetMapping("/fx/orderItem/add")
	@RequiresPermissions("fx:orderItem:add")
	String add(){
	    return "fx/orderItem/add";
	}

	@GetMapping("/fx/orderItem/edit/{itemId}")
	@RequiresPermissions("fx:orderItem:edit")
	String edit(@PathVariable("itemId") Long itemId,Model model){
		OrderItemDO orderItem = orderItemService.get(itemId);
		model.addAttribute("orderItem", orderItem);
	    return "fx/orderItem/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/fx/orderItem/save")
	@RequiresPermissions("fx:orderItem:add")
	public R save( OrderItemDO orderItem){
		if(orderItemService.save(orderItem)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/fx/orderItem/update")
	@RequiresPermissions("fx:orderItem:edit")
	public R update( OrderItemDO orderItem){
		orderItemService.update(orderItem);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/fx/orderItem/remove")
	@ResponseBody
	@RequiresPermissions("fx:orderItem:remove")
	public R remove( Long itemId){
		if(orderItemService.remove(itemId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/fx/orderItem/batchRemove")
	@ResponseBody
	@RequiresPermissions("fx:orderItem:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] itemIds){
		orderItemService.batchRemove(itemIds);
		return R.ok();
	}


	/*
	* 获取购物车商品信息
	* */
	@RequestMapping("/wx/orderItem/getOrderItem")
	@ResponseBody
	public JsonResult getOrderItem(String orderId){
		Map<String,Object> map = new HashMap<>();
		map.put("orderId",orderId);
		return ResultUtil.renderSuccess(orderItemService.list(map));
	}


}
